package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleContants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApArticleServiceImpl implements ApArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    @Override
    public ResponseResult load(ArticleHomeDto dto, Short loadtype) {
        //1. 参数校验
        //1.1 每页展示数据条数校验 , 默认 展示10条, 最多不超过50条
        Integer size = dto.getSize();
        if (size==null||size==0){
            size=10;
        }
        dto.setSize(Math.max(size,50));

        //1.2 时间校验

        if (dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }

        if (dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }

        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleContants.DEFAULT_TAG);
        }

        //1.3 频道
        return ResponseResult.okResult(apArticleMapper.loadArticleList(dto,loadtype));
    }

    @Override
    public ResponseResult save(ArticleDto dto) {
        //参数校验
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);

        //新增
        if (dto.getId()==null){
            //1.1 新增文章基础信息
            apArticleMapper.insert(apArticle);

            //1.2 新增文章配置信息
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            //是否可以评论
            apArticleConfig.setIsComment(true);
            //是否已删除
            apArticleConfig.setIsDelete(false);
            //是否下架
            apArticleConfig.setIsDown(false);
            //是否可以转发
            apArticleConfig.setIsForward(true);

            apArticleConfigMapper.insert(apArticleConfig);
            //1.3 新增文章内容信息
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());

            apArticleContentMapper.insert(apArticleContent);
        }else {
            //2. 修改
            //2.1 修改基础信息
            apArticleMapper.updateById(apArticle);

            //2.2 修改文章内容信息
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.lambdaQuery(ApArticleContent.class).eq(ApArticleContent::getArticleId, apArticle.getId()));
            apArticleContent.setContent(dto.getContent());

            apArticleContentMapper.updateById(apArticleContent);
        }
        //异步调用 生成静态文件上传到minio中
        articleFreemarkerService.buildArticle2Minio(apArticle,dto.getContent());

        return ResponseResult.okResult(apArticle.getId());
    }
}
